package cn.hm.search.controller;

import cn.hm.common.response.Result;
import cn.hm.common.response.StatusCode;
import cn.hm.pay.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付控制器
 * @author Mxb
 * @version 1.0
 * @date 2020/10/2 16:30
 */
@Slf4j
@RestController
@RequestMapping("/weixin/pay")
public class WeixinPayController {
    @Autowired
    private WeixinPayService weixinPayService;

    /***
     * 查询支付状态
     * @param outtradeno
     * @return
     */
    @GetMapping(value = "/status/query")
    public Result queryStatus(String outtradeno) {
        log.info("=========微信支付状态查询开始===========");
        Map<String, String> resultMap = weixinPayService.queryPayStatus(outtradeno);
        return new Result(true, StatusCode.OK, "查询支付状态成功！", resultMap);
    }


    /***
     * 支付回调
     * @param request
     * @return
     */
    @RequestMapping(value = "/notify/url")
    public String notifyUrl(HttpServletRequest request) {
        log.info("========微信支付回调开始start========");
        log.info("回调参数打印：" + request);
        InputStream inStream;
        try {
            //读取支付回调数据  网络数据流
            inStream = request.getInputStream();
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            // 循环读 流中的数据
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            outSteam.close();
            inStream.close();
            // 将支付回调数据转换成xml字符串
            String result = new String(outSteam.toByteArray(), "utf-8");
            System.out.println("======打印响应xml数据====== \n" + result);

            //将xml字符串转换成Map结构
            Map<String, String> map = WXPayUtil.xmlToMap(result);
            System.out.println("======打印响应map数据====== \n" + map);
            //响应给微信的数据 设置
            Map respMap = new HashMap();
            respMap.put("return_code", "SUCCESS");
            respMap.put("return_msg", "OK");
            return WXPayUtil.mapToXml(respMap);
        } catch (Exception e) {
            e.printStackTrace();
            //记录错误日志
        }
        return null;
    }

    /**
     * 统一下单接口
     * 创建二维码
     * @param oriderNo 订单号
     * @param money    总金额
     * @return
     */
    @RequestMapping("/create/native")
    public Result createNative(String oriderNo, String money) {
        Map<String, String> mapNative = this.weixinPayService.createNative(oriderNo, money);
        return new Result(true, StatusCode.OK, "微信下单成功！", mapNative);
    }

}
